﻿using System;
using System.Diagnostics;

namespace ProjectEuler
{
    internal class Problem19
    {
        private int[] monthDays = {
                                      31, //Jan
                                      28, //Feb
                                      31, //Mar
                                      30, //Apr
                                      31, //May
                                      30, //Jun
                                      31, //Jul
                                      31, //Aug
                                      30, //Sep
                                      31, //Oct
                                      30, //Nov
                                      31 //Dec
                                  };

        public void Solve()
        {
            int dayIndex = 1;
            int sundayCount = 0;

            for (int year = 1901; year < 2001; year++)
            {
                for (int month = 0; month < 12; month++)
                {
                    var days = getDays(month, year);
                    dayIndex += days;

                    if (dayIndex % 7 == 0)
                    {
                        sundayCount++;
                        Debug.WriteLine("{0}: {1} / {2} ", sundayCount, year, month+2);
                    }
                }
            }

            Debug.WriteLine("problem 19: " + sundayCount);
        }

        private int getDays(int month, int year)
        {
            if (month == 1)
            {
                if (year % 400 == 0)
                {
                    return monthDays[month] + 1;
                }

                if (year % 4 == 0 )
                {
                    if (year % 100 != 0)
                    {
                        return monthDays[month] + 1;
                    }
                }
            }

            return monthDays[month];
        }
    }
}